namespace TomatoBreak.Infrastructure
{
    using System;
    using System.Web.Mvc;

    using Web;

    public class FiltersRegistrar : IRegistrar
    {
        private readonly FilterRegistry registry;

        public FiltersRegistrar(FilterRegistry registry)
        {
            if (registry == null)
            {
                throw new ArgumentNullException("registry");
            }

            this.registry = registry;
        }

        public void Register()
        {
            var thisAssembly = GetType().Assembly;
            Predicate<Type> isController = type => !type.IsAbstract && typeof(Controller).IsAssignableFrom(type);

            var controllers = new TypeQuery().Add(thisAssembly).Include(isController);

            var userAwareControllers = new TypeQuery().Add(thisAssembly)
                                                      .Include(t => isController(t) && typeof(ICurrentUserAware).IsAssignableFrom(t));

            registry.Register<Transaction>(controllers)
                    .Register<CurrentUser>(userAwareControllers);
        }
    }
}